from panda3d.core import Vec3
import cfg

class FollowCam():
    def __init__(self, parent, camera, target):
        self.dummy = render.attachNewNode("cam" + target.
            getName())
        self.turnRate = 7
        self.moveRate = .5
        self.camera = camera
        self.target = target
        self.mouseTilt = 1
        self.camera.reparentTo(self.target)
        self.camera.setPosHpr(0,90,70,0,20,0)
        self.camera.lookAt(self.target)
        taskMgr.add(self.updateCamera, "updateCamera" + target.getName())

    def updateCamera(self, task):
        if base.mouseWatcherNode.hasMouse():
            self.mouseTilt -= base.mouseWatcherNode.getMouseY() * 3
        if self.mouseTilt < 0:
            self.mouseTilt = 0
        if self.mouseTilt > 1000:
            self.mouseTilt = 1000
        #heading = self.clampAngle(self.dummy.getH())
        #turnDiff = self.target.getH() - heading
        #xDiff = self.target.getX() - self.dummy.getX()
        #yDiff = self.target.getY() - self.dummy.getY()
        #zDiff = self.target.getZ() - self.dummy.getZ()
        #turnDiff = self.clampAngle(turnDiff)
        #dt = globalClock.getDt()
        #turn = turnDiff * dt
        #xDiff *= self.moveRate
        #yDiff *= self.moveRate
        #self.dummy.setX(self.dummy.getX() + xDiff)
        #self.dummy.setY(self.dummy.getY() + yDiff)
        #self.dummy.setZ(self.dummy.getZ() + zDiff)

        #self.dummy.setH(heading + turn * self.turnRate)

        #self.dummy.setH(cfg.player.model.getH())

        #self.camera.setPos(self.dummy.getPos())
        #self.camera.setY(self.dummy, 25)
        self.camera.setZ(20+ self.mouseTilt*5)
        self.camera.lookAt((0,0,self.target.getZ()+10))
        #self.camera.lookAt(self.target.getPos() + Vec3(0, 0, 6))
        return task.cont

    def clampAngle(self, angle):
        while angle < -180:
            angle = angle + 360

        while angle > 180:
            angle = angle - 360

        return angle


